package com.seven.webhook.simple.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gs.cd.cloud.common.ApiResult;
import com.seven.webhook.controller.MonitorRuleController;
import com.seven.webhook.model.MonitorRule;
import com.seven.webhook.service.MonitorRuleService;
import com.seven.webhook.simple.model.MonitorSimpleRule;
import com.seven.webhook.simple.model.enm.BaseIndexEnum;
import com.seven.webhook.simple.model.enm.RuleSymbolEnum;
import com.seven.webhook.simple.service.MonitorSimpleRuleService;
import com.seven.webhook.utils.specification.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author seven
 * @since 2020-11-13
 */
@RestController
@RequestMapping("/webhook/monitorSimpleRule")
public class MonitorSimpleRuleController extends AbstractController<MonitorSimpleRule, MonitorSimpleRuleService> {

    @Autowired
    private MonitorRuleController monitorRuleController;
    @Autowired
    private MonitorRuleService monitorRuleService;

    @Override
    public ApiResult saveOrUpdate(@RequestBody MonitorSimpleRule monitorSimpleRule) {
        //check
        String ruleName = monitorSimpleRule.getRuleName();
        MonitorRule monitorRule = monitorRuleService.getByRuleName(ruleName);
        MonitorRule parse2MonitorRule = monitorSimpleRule.parse2MonitorRule();
        parse2MonitorRule.setId(monitorRule==null?null:monitorRule.getId());
        monitorRuleController.saveOrUpdate(monitorRule);
        return super.saveOrUpdate(monitorSimpleRule);
    }

    /**
     * 获取监控指标
     */
    @GetMapping("get/baseIndex")
    public ApiResult getBaseIndex() {
        BaseIndexEnum[] values = BaseIndexEnum.values();
        return ApiResult.success(values);
    }

    /**
     * 获取监控规则符号
     */
    @GetMapping("get/ruleSymbol")
    public ApiResult getRuleSymbol() {
        RuleSymbolEnum[] values = RuleSymbolEnum.values();
        return ApiResult.success(values);
    }

    /**
     * 开启告警
     */
    @GetMapping("alert/{id}")
    public ApiResult alert(@PathVariable String id) {
        MonitorSimpleRule byId = iService.getById(id);
        MonitorRule monitorRule = monitorRuleService.getByRuleName(byId.getRuleName());
        if (monitorRule==null){return ApiResult.error();}
        monitorRuleController.alert(monitorRule.getId());
        byId.setIsAlert(true);
        boolean b = iService.updateById(byId);
        return b ? ApiResult.success() : ApiResult.error();
    }

    /**
     * 关闭告警
     */
    @GetMapping("unalert/{id}")
    public ApiResult unalert(@PathVariable String id) {
        MonitorSimpleRule byId = iService.getById(id);
        MonitorRule monitorRule = monitorRuleService.getByRuleName(byId.getRuleName());
        monitorRuleController.alert(monitorRule.getId());
        byId.setIsAlert(false);
        boolean b = iService.updateById(byId);
        return b ? ApiResult.success() : ApiResult.error();
    }

}

